package kr.co.cherryt.view;

import kr.co.cherryt.activity.DetailPopupActivity;
import kr.co.cherryt.common.CC_Log;
import kr.co.cherryt.common.CherrytData;
import kr.co.cherryt.data.unit.UserInfo;
import kr.co.cherryt.layout.GalleryItemLayout;
import kr.co.cherryt.listView.BaseListView;
import kr.co.cherryt.listView.item.BaseItemLayout;
import kr.co.cherryt.parser.xml.XmlManager;
import kr.co.cherryt.parser.xml.XmlParserCore;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.AttributeSet;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

public class GalleryListView extends BaseListView {
	
	public GalleryListView(Context context) {
		super(context);
		initControl(context);
	}

	public GalleryListView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initControl(context);
	}
	
	private void initControl(Context context) {
		// 탭바에 리스트가 가려져 하단에 padding 주기
		setPadding(0, 0, 0, 20);
	}
	
	public void fillListViewInner(UserInfo userInfo, int mAdListPosition){
		
		String szUrl = CherrytData.getXmlUrl(getContext(), "ct_ad_list.php", "os=A&mode=1&user_id=" + userInfo.getUserID());
		
		CC_Log.info("jin", "기업광고 리스트 : " + szUrl);
		
		setListItemUrl(szUrl);
	}
	
	@Override
	public View customGetView(int position, View convertView, ViewGroup parent, XmlManager parser) {
		
		Context context = getContext();
		BaseItemLayout layout = null;

		if(convertView == null) {
			layout = new GalleryItemLayout(context, m_childClickListener, true);
		}
		else {
			layout = (BaseItemLayout)convertView;
		}
		
		// item 상단 (시청한 광고, 시청 안한 광고) 노출
		LinearLayout layoutItemHeader = (LinearLayout)layout.findViewById(BaseItemLayout.CTRL_ID_ITEM_LAYOUT_HEADER);
		TextView textItemHeader = (TextView)layout.findViewById(BaseItemLayout.CTRL_ID_ITEM_TEXT_HEADER);
		
		int nTotalCnt =  XmlParserCore.getXmlTotalCnt(parser);
		int nSaveCnt =  XmlParserCore.getXmlSaveCnt(parser);
		
		if (textItemHeader != null && layoutItemHeader != null) {
			// 전체 광고 수가 시청한 광고 수보다 많으면 첫번째 헤더는 [시청 안한 광고]
			if (nTotalCnt > nSaveCnt) {
				if (position == 0) {
					textItemHeader.setText("시청 안한 광고");
					layoutItemHeader.setVisibility(View.VISIBLE);
				}
				else if (position == (nTotalCnt - nSaveCnt)){
					textItemHeader.setText("시청한 광고");
					layoutItemHeader.setVisibility(View.VISIBLE);
				}
				else {
					layoutItemHeader.setVisibility(View.GONE);
				}
			}
			else {
				if (position == 0) {
					textItemHeader.setText("시청한 광고");
					layoutItemHeader.setVisibility(View.VISIBLE);
				}
				else {
					layoutItemHeader.setVisibility(View.GONE);
				}
			}
		}
		
		layout.fillServerData(context, parser, position);
		layout.m_nUniqueIdx = position;

		return layout;	
	}
	
	/**
	 * ListView에서 공통으로 사용될 클릭 리스너
	 */
	private OnClickListener m_childClickListener = new OnClickListener() {
		
		@Override
		public void onClick(View v) {

			int nIdx = BaseItemLayout.getLayoutId(v);
			
			if(nIdx > -1){
				onLayoutClick(v, nIdx, v.getId());
			}
		}
	};
	
	/**
	 * ListView와 childView의 클릭 리스너를 동시에 처리하기 위한 함수
	 * @param view - childView 또는 ListView의 Layout
	 * @param position - 클릭된 List의 position
	 * @param id - chiildView인 경우 해당 view의 id, ListView인 경우 -1
	 */
	public void onLayoutClick(View view, int position, long id){ 
		Context context = getContext();
		
		//if (id == BaseItemLayout.CTRL_ID_TITLE_TEXT || id == BaseItemLayout.CTRL_ID_THUMB_IMAGE) {
			CC_Log.info("jin", "리스트 클릭!!! : " + position);
			
			String szNum = XmlParserCore.getXmlItemElementData(getXmlManager(), position, "num");
			String szImageUrl = XmlParserCore.getXmlItemElementData(getXmlManager(), position, "img_url");
			String szTitle = XmlParserCore.getXmlItemElementData(getXmlManager(), position, "title");
			String szContent = XmlParserCore.getXmlItemElementData(getXmlManager(), position, "content");
			String szCompanyName = XmlParserCore.getXmlItemElementData(getXmlManager(), position, "company_name");
			String szPoint = XmlParserCore.getXmlItemElementData(getXmlManager(), position, "point");
			String szLinkUrl = XmlParserCore.getXmlItemElementData(getXmlManager(), position, "link_url");
			String szMovieUrl = XmlParserCore.getXmlItemElementData(getXmlManager(), position, "movie_url");
			String szPointSaveType = XmlParserCore.getXmlItemElementData(getXmlManager(), position, "point_save_type");
			String szPackageName = XmlParserCore.getXmlItemElementData(getXmlManager(), position, "package_name");
			String szPackageTitle = XmlParserCore.getXmlItemElementData(getXmlManager(), position, "package_title");
			
			Intent intent = new Intent(context, DetailPopupActivity.class);
			intent.putExtra("num", szNum);
			intent.putExtra("img_url", szImageUrl);
			intent.putExtra("title", szTitle);
			intent.putExtra("content", szContent);
			intent.putExtra("company_name", szCompanyName);
			intent.putExtra("point", szPoint);
			intent.putExtra("point_save_type", szPointSaveType);
			intent.putExtra("link_url", szLinkUrl);
			intent.putExtra("movie_url", szMovieUrl);
			intent.putExtra("package_name", szPackageName);
			intent.putExtra("package_title", szPackageTitle);
			((Activity) context).startActivityForResult(intent, CherrytData.GALLERY_DETAIL_RESULT_CODE);
		//}
	}
}
